package com.atguigu.gmall.config;

import com.alibaba.dubbo.config.*;
import com.atguigu.gmall.service.UserService;
import com.atguigu.gmall.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

@Configuration  //标注本类为配置类
public class MyDubboConfig {

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("04-boot-user-service-provider");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        return registryConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        return protocolConfig;
    }

    @Bean
    public ServiceConfig<UserService> serviceServiceConfig(UserServiceImpl userServiceImpl){
        ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<>();
//        userServiceServiceConfig.setInterface("com.atguigu.gmall.service.UserService");//方式一
        userServiceServiceConfig.setInterface(UserService.class);//方式二
        userServiceServiceConfig.setRef(userServiceImpl);
        userServiceServiceConfig.setVersion("1.0.0");

        //配置每一个方法的的信息
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("getUserAddressList");
        methodConfig.setTimeout(5000);//5秒
        //将方法配置关联到Service中去
        List<MethodConfig> methods = new ArrayList<>();
        methods.add(methodConfig);
        userServiceServiceConfig.setMethods(methods);

        return userServiceServiceConfig;
    }

//    @Bean
//    public UserServiceImpl userServiceImpl(){
//        return new UserServiceImpl();
//    }

    @Bean
    public MonitorConfig monitorConfig(){
        MonitorConfig monitorConfig = new MonitorConfig();
        monitorConfig.setProtocol("registry");
        return monitorConfig;
    }
}
